<div class="panel-manage">
	<div class="navbar navbar-inverse">
		<div class="navbar-inner">		
			<div class="text-right">		
		    	<button class="btn btn-primary" id="btn-save">บันทึก</button>
		    </div>
		</div>
	</div>
	<div class="panel-message"></div>
	<div style="margin-top:20px;">		
		<div class="well well-small"><b>ข้อมูลเข้าสู่ระบบ</b></div>		
		<div class="section">			
			<div class="control-group row-fluid">
				<label class="control-label span4">ชื่อผู้ใช้: </label>
				<div class="span8">
					<div class="controls">
						<div style="padding-bottom:10px;"><b><?php print $item['MEM_EMAIL']; ?></b></div>
					</div>
					<div class="alert alert-info"><p><i class="icon-info-sign"></i><small> หากท่านลงทะเบียนใช้งานด้วย Facebook ท่านสามารถใช้อีเมล์นี้ Log in เข้าสู่ระบบได้เช่นกัน</small></p></div>
				</div>
			</div>
		</div>				
			
		<div class="well well-small"><b>เปลี่ยนรหัสผ่าน</b></div>
		<form id="frm-account" method="post" action="<?php print base_url() . "member/account/ajax_change_password"; ?>">
			<div class="section">	
			
				<div class="control-group row-fluid" id="group-aop">
					<label class="control-label span4" for="aop">รหัสผ่านเก่า: </label>
				<?php if (!empty($item['MEM_PASS'])): ?>
					<div class="span8">
						<div class="controls">
							<input type="password" name="aop" id="aop" />
						</div>
						<div id="panel-aop-message"></div>
					</div>								
					<input type="hidden" name="pset" value="1" />					
				<?php else: ?>
					<div class="span8">						
						<div class="alert alert-warning"><p><i class="icon-warning-sign"></i><small> ท่านยังไม่เคยตั้งรหัสผ่าน</small></p></div>						
					</div>
					<input type="hidden" name="pset" value="0" />				
				<?php endif; ?>
				</div>
				<div class="control-group row-fluid" id="group-anp">
					<label class="control-label span4" for="anp">รหัสผ่านใหม่: </label>
					<div class="span8">
						<div class="controls">
							<input type="password" name="anp" id="anp" />
						</div>	
						<div id="panel-anp-message"></div>					
					</div>
				</div>
				<div class="control-group row-fluid" id="group-anpc">
					<label class="control-label span4" for="anpc">ยืนยันรหัสผ่านใหม่: </label>
					<div class="span8">
						<div class="controls">
							<input type="password" id="anpc" />
						</div>
						<div id="panel-anpc-message"></div>
					</div>
				</div>
			</div>
		</form>		
	</div>
</div>
<script type="text/javascript">
	function validate_password(str){
		var regex = /^.*(?=.{6,})(?=.*[a-zA-Z])(?=.*\d).*$/;
		if (regex.test(str))
			return true;
		else
			return false;	
	}
	function fade_alert_dialog(input_sender, alert_dialog, group){
		input_sender.focus(function(){
			alert_dialog.fadeOut(500);		
			group.removeClass('error');	
		});
	}
	$(function(){
		$('#btn-save').click(function(){		
			$('#frm-account').ajaxSubmit({
				beforeSubmit	: function(){
					if ($('input[name=pset]').val() == 1){
						if ($('#aop').val().length <= 0){
							var html = '<div class="alert alert-error alert-aop-message">';
							html += '<button type="button" class="close" data-dismiss="alert-aop-message">&times;</button>';
							html += '<p><small>ท่านยังไม่ได้กรอกรหัสผ่านเก่า</small></p></div>';
							
							$('#panel-aop-message').html(html);
							$('#group-aop').addClass('error');
							fade_alert_dialog($('input[name=aop]'), $('.alert-aop-message'), $('#group-aop'));											
							return false;
						}
					}
					if (!validate_password($('#anp').val())){
						var html = '<div class="alert alert-error alert-anp-message">';
						html += '<button type="button" class="close" data-dismiss="alert-anp-message">&times;</button>';
						html += '<p><small>รหัสผ่านควรประกอบด้วยอักขระภาษาอังกฤษและตัวเลขตั้งแต่ 6 อักขระขึ้นไป</small></p></div>';
						
						$('#panel-anp-message').html(html);
						$('#group-anp').addClass('error');
						fade_alert_dialog($('input[name=anp]'), $('.alert-anp-message'), $('#group-anp'));											
						return false;
					}
					if ($('#anp').val() !== $('#anpc').val()){
						var html = '<div class="alert alert-error alert-anpc-message">';
						html += '<button type="button" class="close" data-dismiss="alert-anpc-message">&times;</button>';
						html += '<p><small>รหัสผ่านไม่ตรงกัน</small></p></div>';

						$('#panel-anpc-message').html(html);
						$('#group-anpc').addClass('error');
						fade_alert_dialog($('#anpc'), $('.alert-anpc-message'), $('#group-anpc'));
						return false;
					}					
				},
				success		: function(output){
					output = JSON.parse(output);						
							
					if (output.result)
						html = '<div class="alert alert-success alert-result">' + output.message + '</div>';
					else
						html = '<div class="alert alert-error alert-result">' + output.message + '</div>';
						
					$('.panel-message').html(html);
					autoclosealert('.alert-result', 2000);
				}
			});
		});
	});
</script>